﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    public partial class CommonCode : BusinessBase
    {
        DataClassesDataContext db = new DataClassesDataContext();

        public List<CHCommonCode> LoadDataCommonByCodeStringAndCodeValue(string type, string codeString, string codeValue)
        {
            var query = from a in db.CHCommonCodes
                        where a.CodeKey.Contains(type) && (!string.IsNullOrEmpty(codeString) ? a.CodeString.Equals(codeString) : true)
                        && (!string.IsNullOrEmpty(codeValue) ? a.CodeValue.Contains(codeValue) : true)
                        orderby a.CodeValue ascending
                        select a;
            return query.ToList();
        }

        public static List<CHCommonCode> GetXepHangDonVi()
        {
            return (new CommonCode()).LoadDataCommonByCodeKey("XepHangDonVi");
        }

        public static List<CHCommonCode> GetKhoiQuanlyToChuc()
        {
            return (new CommonCode()).LoadDataCommonByCodeKey("KhoiQuanlyToChuc");
        }

        public static List<CHCommonCode> GetLoaiToChuc()
        {
            return (new CommonCode()).LoadDataCommonByCodeKey("LoaiToChuc");
        }


        public List<CHCommonCode> LoadDataCommonByCodeKey(string type)
        {
            var query = from a in db.CHCommonCodes
                        where a.CodeKey.Trim().Equals(type)
                        orderby a.CodeID ascending
                        select a;
            foreach (CHCommonCode item in query)
            {
                item.TenMaCha = item.CodeIDCha > 0 ? this.LoadCommonCodeByCodeId(item.CodeIDCha).CodeValue : string.Empty;
                item.TenCodeIdType = item.CodeIDType.HasValue && item.CodeIDType.Value > 0 ? this.LoadCommonCodeByCodeId(item.CodeIDType.Value).CodeValue : string.Empty;
            }
            return query.ToList();
        }
        public List<CHCommonCode> LoadDataByCodeIDCha(int codeId)
        {
            var query = from a in db.CHCommonCodes
                        where a.CodeIDCha.Equals(codeId)
                        orderby a.CodeValue
                        select a;
            return query.ToList();
        }
        public string LoadCommonCodeById(int codeId)
        {
            var query = from a in db.CHCommonCodes
                        where a.CodeID.Equals(codeId)
                        orderby a.CodeValue
                        select a;
            if (query != null && query.Count() > 0)
            {
                return query.SingleOrDefault().CodeValue;
            }
            return string.Empty;
        }
        public string LoadCodeStringById(int codeId)
        {
            var query = from a in db.CHCommonCodes
                        where a.CodeID.Equals(codeId)
                        orderby a.CodeString
                        select a;
            if (query != null && query.Count() > 0)
            {
                return query.SingleOrDefault().CodeString;
            }
            return string.Empty;
        }

        public CHCommonCode LoadCommonCodeByCodeString(string codeString, string type)
        {
            var query = (from a in db.CHCommonCodes
                         where a.CodeString.Equals(codeString) && a.CodeKey.Equals(type)
                         orderby a.CodeID
                         select a).SingleOrDefault();
            return query;
        }
        public CHCommonCode LoadCommonCodeByCodeId(int codeId)
        {
            var query = (from a in db.CHCommonCodes
                         where a.CodeID.Equals(codeId)
                         select a).SingleOrDefault();
            return query;
        }
        public List<CHToChucHanhChinh> LoadToChucHanhChinhByLoai(int loai)
        {
            var query = from a in db.CHToChucHanhChinhs
                        where a.LoaiToChucHanhChinh.Equals(loai)
                        select a;
            return query.ToList();
        }
        public List<CHToChucHanhChinh> LoadToChucHanhChinhByToChucCha(int idCha)
        {
            var query = from a in db.CHToChucHanhChinhs
                        where a.ToChucHanhChinhIDCha.Equals(idCha)
                        select a;
            return query.ToList();
        }
        public string LoadTenHanhChinhById(int codeId)
        {
            var query = from a in db.CHToChucHanhChinhs
                        where a.ToChucHanhChinhID.Equals(codeId)
                        select a;
            if (query != null && query.Count() > 0)
            {
                return query.SingleOrDefault().TenToChucHanhChinh;
            }
            return string.Empty;
        }
        public void UpdateCommonCode(int codeId, int idCha, int codeIdType, string codeString, string ghiChu, string codeValue)
        {
            CHCommonCode item = this.Context.CHCommonCodes.Where(hr => hr.CodeID == codeId).FirstOrDefault();
            item.CodeIDCha = idCha;
            item.CodeString = codeString;
            item.GhiChu = ghiChu;
            item.CodeValue = codeValue;
            item.CodeIDType = codeIdType;
            this.Context.SubmitChanges();
        }
        public void InsertCommonCode(int idCha, string codeString, string ghiChu, string codeValue, string codeKey)
        {
            CHCommonCode item = new CHCommonCode();
            item.CodeIDCha = idCha;
            item.CodeString = codeString;
            item.GhiChu = ghiChu;
            item.CodeValue = codeValue;
            item.CodeKey = codeKey;
            this.Context.CHCommonCodes.InsertOnSubmit(item);
            this.Context.SubmitChanges();
        }
        public void DeleteCommonCode(int id)
        {
            //xóa dữ liệu các bảng con theo nhân viên.

            //Xóa nhân viên
            using (var context = new DataClassesDataContext())
            {
                var item = context.CHCommonCodes.Single(a => a.CodeID.Equals(id));
                context.CHCommonCodes.DeleteOnSubmit(item);
                context.SubmitChanges();
            }

        }
        public string LoaiMa = "LoaiMa";
        public string TrangThaiLamViec = "TrangThaiLamViec";
        public string BacLuong = "BacLuong";
        public string NgachCongChuc = "NgachCongChuc";
        public string LoaiHopDong = "LoaiHopDong";
        public string NgheNghiep = "NgheNghiep";
        public string DanToc = "DanToc";
        public string TonGiao = "TonGiao";
        public string GioiTinh = "GioiTinh";
        public string TrinhDoGiaoDucPhoThong = "TrinhDoGiaoDucPhoThong";
        public string HeGiaoDuc = "HeGiaoDuc";
        public string NhomMau = "NhomMau";
        public string TrinhDoChuyenMon = "TrinhDoChuyenMon";
        public string DoiTuongChinhSach = "DoiTuongChinhSach";
        public string HinhThucDaoTao = "HinhThucDaoTao";
        public string QuanHam = "QuanHam";
        public string DanhHieu = "DanhHieu";
        public string LoaiDonViHanhChinh = "LoaiDonViHanhChinh";
        public string LoaiBangCap = "LoaiBangCap";
        public string XepHangDonVi = "XepHangDonVi";
        public DateTime Default = Convert.ToDateTime("01/01/1755");
        public string LyLuanChinhTri = "LyLuanChinhTri";
        public string TrinhDoTinHoc = "TrinhDoTinHoc";
        public string TrinhDoNgoaiNgu = "TrinhDoNgoaiNgu";
        public string QuanLyNhaNuoc = "QuanLyNhaNuoc";
        public string PhuCapChucVu = "PhuCapChucVu";
        public string ChuyenNganh = "ChuyenNganh";
        public string LoaiNhomNguoiDung = "LoaiNhomNguoiDung";
        public string KhoiQuanlyToChuc = "KhoiQuanlyToChuc";
        public string LoaiToChuc = "LoaiToChuc";
        public string CapKiLuat = "CapKiLuat";
        public string CommandReset = "Reset";
        public string CommandTaoMoi = "TaoMoi";
        public string CommandSua = "Sua";
        public string CommandXoa = "Xoa";
        public string CommandLoadByID = "LoadByID";
        public string QuaTrinhCongTac = "1";
        public string HopDong = "2";
        public string KhenThuong = "3";
        public string KiLuat = "4";

    }
}
